var dailyTemperatures = function(temperatures) {
  const len = temperatures.length;
  let stack = [];
  let ans = new Array(len).fill(0);

  for (let i = len - 1; i >= 0; i--) {
    console.log(stack);
    const temp = temperatures[i];
    while (stack.length && temp >= temperatures[stack[stack.length - 1]]) {
      stack.pop();
    }
    if (stack.length) {
      ans[i] = stack[stack.length - 1] - i;
    }
    stack.push(i);
  }
    
  return ans;
};

const temperatures = [89,62,70,58,47,47,46,76,100,70];
console.log(dailyTemperatures(temperatures)); // [1,1,4,2,1,1,0,0]